package com.qunar.homework4.controller;

import com.qunar.homework4.model.Paragraph;
import com.qunar.homework4.model.vo.CounterResult;
import com.qunar.homework4.service.ContentCounterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/")
public class ContentCounterController {

    private static final String TITLE_FIELD = "";
    private static final String CONTENT_FIELD = "";
    @Resource
    private ContentCounterService contentCounterService;

    @RequestMapping(value = "/api/counter", method = RequestMethod.POST)
    public String counterParagraph(@Param(value = "url") String url){
        log.info("url is {}", url);
        CounterResult<Paragraph> result = contentCounterService.counterContent(url, TITLE_FIELD, CONTENT_FIELD);
        log.info("counter result is {}", result.toString());
        return "redirect:/queryAll";
    }

    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public String queryAll(Model model){
        CounterResult<List<Paragraph>> queryAllResult = contentCounterService.queryAllCounterResult();
        log.info("queryAll result is {}", queryAllResult.toString());
        model.addAttribute("result", queryAllResult);
        return "index";
    }
}
